package com.lxb.eduservice.controller;


import com.lxb.commonutils.R;
import com.lxb.eduservice.entity.EduChapter;
import com.lxb.eduservice.service.EduChapterService;
import com.lxb.eduservice.vo.chapter.ChapterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程章节
 *
 * @author lixuebin
 * @since 2022-11-06
 */
@Api(description = "课程章节")
@RestController
@RequestMapping("/eduservice/chapter")
//@CrossOrigin
public class EduChapterController {

    @Autowired
    EduChapterService chapterService;

    /**
     * 获取课程大纲列表，根据课程id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "获取课程大纲列表")
    @GetMapping("/getChapterVideo/{id}")
    public R getChapterVideo(
            @ApiParam(value = "课程id", defaultValue = "18", required = true)
            @PathVariable("id") String id) {
        List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(id);
        return R.ok().data("allChapterVideo", chapterVoList);
    }


    @ApiOperation(value = "添加章节")
    @PostMapping("/addChapter")
    public R addChapter(
            @ApiParam(name = "eduChapter", value = "章节对象", required = true)
            @RequestBody EduChapter eduChapter) {

        boolean add = chapterService.save(eduChapter);
        if (!add) {
            return R.error().message("添加失败");
        }
        return R.ok();

    }

    @ApiOperation(value = "根据章节id   查询章节")
    @GetMapping("/getChapterById/{chapterId}")
    public R getChapterById(
            @ApiParam(name = "chapterId", value = "章节id", required = true)
            @PathVariable String chapterId) {

        EduChapter eduChapter = chapterService.getById(chapterId);
        if (eduChapter != null) {
            return R.ok().data("eduChapter", eduChapter);
        }
        return R.error().message(chapterId + "查询失败");
    }

    @ApiOperation(value = "根据id更新章节")
    @PostMapping("/updateByIds")
    public R updateById(
            @ApiParam(name = "eduChapter", value = "章节对象", required = true)
            @RequestBody EduChapter eduChapter) {

        boolean update = chapterService.updateById(eduChapter);

        if (!update) {
            return R.error().message("更新失败");
        }
        return R.ok();
    }

    @ApiOperation(value = "按id删除章节")
    @DeleteMapping("/deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable("chapterId") String chapterId) {
        System.out.println("wwww");
        //当改章节下有小节信息，不删除
        boolean deleStatu = chapterService.deleteChapter(chapterId);
        if(deleStatu){
            return R.ok();
        }else {
            return R.error().message("删除失败");
        }
    }
}

